<?php

namespace Admin\Controller;

class CustomerController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Customer");
    }

    //首页
    public function index() {

//        $requestMap = $this->convertRequestToMap();
        $order = null;

        if($_REQUEST["c#disease"] != 0){
            $order = "cus.disease_" . $_REQUEST["c#disease"]. " desc";
        }else{
            $requestMap = $this->convertRequestToMap();
        }
        //分页

        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取customer列表

        $userList = $this->currentModel->queryForList($page, $requestMap, $order);

        $summary = "";

        $totalAccount = D("Customer")->count();

        $summary .= "总用户".$totalAccount."人，其中，";

        $categoryList = D("Category")->select();

        foreach ($categoryList as $category) {
            $count = D("Customer")
                ->where(array("category_id"=>$category["category_id"]))
                ->count();
            $summary .= $category["category_name"]."患者".$count."人。";
        }

        $this->assign("summary", $summary);
        $this->assign("diseaseEnum", C("DISEASE_STATUS"));
        $this->assign("categoryIdEnum", C("CATEGORY_ID_STATUS"));
        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('userList', $userList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function updateInfo() {
        $id = $_REQUEST["id"];
        $customer = $this->currentModel->queryById($id);

        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".access_token()."&openid=".$customer["openid"]."&lang=zh_CN";
        $info = json_decode(curl($url),true);

        $nicknameTmp = json_encode($info["nickname"]);
        $nicknameTmp = preg_replace_callback("#(\\\ud[0-9a-f]{3})#i",function($r){ return ""; },$nicknameTmp);
        $nicknameNoEmoji = json_decode($nicknameTmp);

        $customer["realname"] = $nicknameNoEmoji;
        $customer["headimgurl"] = $info["headimgurl"];

        D("Customer")->updateInfo($customer);
        $this->success("刷新用户信息成功", U("customer/index"));
    }

    public function becomeCompany() {

        $id = $_REQUEST["id"];

        $customer = $this->currentModel->queryById($id);

        $companyMap["customer_id"] = $customer["customer_id"];
        $company = D("Company")->queryByMap($companyMap);

        if(isNullOrEmpty($company)) {

            $companyData["create_time"] = getCurrentDate();
            $companyData["company_name"] = $customer["nickname"];
            $companyData["customer_id"] = $customer["customer_id"];
            D("Company")->add($companyData);

            $customer["is_company"] = C("DEFAULT_YN.YES");
            D("Customer")->updateInfo($customer);
        }

        $this->success("添加成功", U("customer/index"));

    }

    public function becomeDoctor() {

        $id = $_REQUEST["id"];

        $customer = $this->currentModel->queryById($id);

        $categoryList = D("Category")->select();

        $this->assign("categoryList", $categoryList);
        $this->assign("info", $customer);
        $this->display("becomeDoctor");

    }

    public function addDoctor() {

        $doctorMap["customer_id"] = $_REQUEST["customerId"];

        $doctor = D("Doctor")->queryByMap($doctorMap);

        if(!isNullOrEmpty($doctor)) {
            $this->error("已经存在该医生");exit();
        }

        $doctorData["customer_id"] = $_REQUEST["customerId"];
        $doctorData["doctor_name"] = $_REQUEST["doctorName"];
        $doctorData["hospital_name"] = $_REQUEST["hospitalName"];
        $doctorData["image_url"] = $_REQUEST["imageUrl"];
        $doctorData["city_id"] = $_REQUEST["cityId"];
        $doctorData["create_time"] = getCurrentDate();
        //$doctorData["description"] = html_entity_decode($_REQUEST["description"]);
        $doctorData["good_at"] = html_entity_decode($_REQUEST["goodAt"]);
        $doctorData["expirence"] = html_entity_decode($_REQUEST["expirence"]);
        $doctorData["doctor_link"] = $_REQUEST["doctorLink"];
        $doctorData["brief"] = $_REQUEST["brief"];

        $doctorId = D("Doctor")->add($doctorData);

        $categoryIds = I('post.categoryIds');

        $categoryIdArray = explode(',', $categoryIds);

        foreach ($categoryIdArray as $categoryId) {

            $doctorCategoryData["doctor_id"] = $doctorId;
            $doctorCategoryData["category_id"] = $categoryId;
            $doctorCategoryData["create_time"] = getCurrentDate();

            D("DoctorCategory")->add($doctorCategoryData);

        }

        $customer = $this->currentModel->queryById($_REQUEST["customerId"]);
        $customer["is_doctor"] = C("DEFAULT_YN.YES");
        D("Customer")->updateInfo($customer);

        $this->success("添加成功", U("customer/index"));

    }

}